package com.primb.bench.common.dialog;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.flyco.dialog.utils.CornerUtils;
import com.flyco.dialog.widget.internal.BaseAlertDialog;
import com.primb.baselibs.view.RecycleViewDivider;
import com.primb.bench.R;
import com.primb.bench.project.approval.entity.NextPersonEntity;
import com.primb.bench.project.home.entity.ApplyNextUserEntity;

import java.util.List;


/**
 * Created by Chen on 2017/7/28.
 * 功能描述：
 */

public class ListDialog extends BaseAlertDialog<ListDialog> {
    /**
     * title underline
     */
    private View mVLineTitle;
    /**
     * vertical line between btns
     */
    private View mVLineVertical;
    /**
     * vertical line between btns
     */
    private View mVLineVertical2;
    /**
     * horizontal line above btns
     */
    private View mVLineHorizontal;
    /**
     * title underline color(标题下划线颜色)
     */
    private int mTitleLineColor = Color.parseColor("#0977ed");
    /**
     * title underline height(标题下划线高度)
     */
    private float mTitleLineHeight = 0.8f;
    /**
     * btn divider line color(对话框之间的分割线颜色(水平+垂直))
     */
    private int mDividerColor = Color.parseColor("#DCDCDC");

    public static final int STYLE_ONE = 0;
    public static final int STYLE_TWO = 1;
    private int mStyle = STYLE_ONE;
    private RecyclerView recyclerView;
    private BaseQuickAdapter<NextPersonEntity, BaseViewHolder> adapter;
    public List<NextPersonEntity> dataList;
    private int position;
    private Context context;
    private OnItemClickL onItemClickL;

    public ListDialog(Context context, List<NextPersonEntity> dataList) {
        super(context);
        widthScale(0.8f);
        NextPersonEntity bean = new NextPersonEntity();
        bean.setPhone("手机号");
        bean.setUserName("姓名");
        bean.setUserId("员工号");
        dataList.add(0, bean);
        this.dataList = dataList;

        this.context = context;
        recyclerView = new RecyclerView(context);

        /** default value*/
        mTitleTextColor = Color.parseColor("#0977ed");
        mTitleTextSize = 18f;
        mContentTextColor = Color.parseColor("#383838");
        mContentTextSize = 17f;
        mLeftBtnTextColor = Color.parseColor("#8a000000");
        mRightBtnTextColor = Color.parseColor("#8a000000");
        mMiddleBtnTextColor = Color.parseColor("#8a000000");
    }

    @Override
    public View onCreateView() {
        /** title */
        mTvTitle.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        mLlContainer.addView(mTvTitle);

        /** title underline */
        mVLineTitle = new View(mContext);
        mLlContainer.addView(mVLineTitle);

        /** content */
        recyclerView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dp2px(250)));
        mLlContainer.addView(recyclerView);

        mVLineHorizontal = new View(mContext);
        mVLineHorizontal.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        mLlContainer.addView(mVLineHorizontal);

        /** btns */
        mTvBtnLeft.setLayoutParams(new LinearLayout.LayoutParams(0, dp2px(45), 1));
        mLlBtns.addView(mTvBtnLeft);

        mVLineVertical = new View(mContext);
        mVLineVertical.setLayoutParams(new LinearLayout.LayoutParams(1, LinearLayout.LayoutParams.MATCH_PARENT));
        mLlBtns.addView(mVLineVertical);

        mTvBtnMiddle.setLayoutParams(new LinearLayout.LayoutParams(0, dp2px(45), 1));
        mLlBtns.addView(mTvBtnMiddle);

        mVLineVertical2 = new View(mContext);
        mVLineVertical2.setLayoutParams(new LinearLayout.LayoutParams(1, LinearLayout.LayoutParams.MATCH_PARENT));
        mLlBtns.addView(mVLineVertical2);

        mTvBtnRight.setLayoutParams(new LinearLayout.LayoutParams(0, dp2px(45), 1));
        mLlBtns.addView(mTvBtnRight);

        mLlContainer.addView(mLlBtns);

        return mLlContainer;
    }

    @Override
    public void setUiBeforShow() {
        super.setUiBeforShow();
        /** title */
        if (mStyle == STYLE_ONE) {
            mTvTitle.setMinHeight(dp2px(48));
            mTvTitle.setGravity(Gravity.CENTER_VERTICAL);
            mTvTitle.setPadding(dp2px(15), dp2px(5), dp2px(0), dp2px(5));
            mTvTitle.setVisibility(mIsTitleShow ? View.VISIBLE : View.GONE);
        } else if (mStyle == STYLE_TWO) {
            mTvTitle.setGravity(Gravity.CENTER);
            mTvTitle.setPadding(dp2px(0), dp2px(15), dp2px(0), dp2px(15));
            mTvTitle.setVisibility(View.VISIBLE);
            mTvTitle.setText(mTitle);
        }

        /** title underline */
        mVLineTitle.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                dp2px(mTitleLineHeight)));
        mVLineTitle.setBackgroundColor(mTitleLineColor);
//        mVLineTitle.setVisibility(mIsTitleShow && mStyle == STYLE_ONE ? View.VISIBLE : View.GONE);


        recyclerView.setPadding(dp2px(15), dp2px(7), dp2px(15), dp2px(20));


        /** btns */
        mVLineHorizontal.setBackgroundColor(mDividerColor);
        mVLineVertical.setBackgroundColor(mDividerColor);
        mVLineVertical2.setBackgroundColor(mDividerColor);

        if (mBtnNum == 1) {
            mTvBtnLeft.setVisibility(View.GONE);
            mTvBtnRight.setVisibility(View.GONE);
            mVLineVertical.setVisibility(View.GONE);
            mVLineVertical2.setVisibility(View.GONE);
        } else if (mBtnNum == 2) {
            mTvBtnMiddle.setVisibility(View.GONE);
            mVLineVertical.setVisibility(View.GONE);
        }

        mTvBtnRight.setOnClickListener(v -> {
            if (onItemClickL != null) {
                onItemClickL.onItemClick(dataList.get(position), position);
            } else {
                dismiss();
            }
        });

        /**set background color and corner radius */
        float radius = dp2px(mCornerRadius);
        mLlContainer.setBackgroundDrawable(CornerUtils.cornerDrawable(mBgColor, radius));
        mTvBtnLeft.setBackgroundDrawable(CornerUtils.btnSelector(radius, mBgColor, mBtnPressColor, 0));
        mTvBtnRight.setBackgroundDrawable(CornerUtils.btnSelector(radius, mBgColor, mBtnPressColor, 1));
        mTvBtnMiddle.setBackgroundDrawable(CornerUtils.btnSelector(mBtnNum == 1 ? radius : 0, mBgColor, mBtnPressColor, -1));

        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayoutManager.VERTICAL));
        adapter = new BaseQuickAdapter<NextPersonEntity, BaseViewHolder>(R.layout.item_dialog_person, dataList) {
            @Override
            protected void convert(BaseViewHolder helper, NextPersonEntity item) {

                ImageView check = helper.getView(R.id.iv_item_dialog_select);
                if (0 == helper.getAdapterPosition()) {
                    helper.setVisible(R.id.layout_item_dialog, false);
                    helper.setVisible(R.id.layout_item_dialog_title, true);
                    helper.setText(R.id.tv_item_dialog_name_title, item.getUserName());
                    helper.setText(R.id.tv_item_dialog_number_title, item.getUserId());
                    helper.setText(R.id.tv_item_dialog_phone_title, item.getPhone());
                } else {
                    helper.setVisible(R.id.layout_item_dialog, true);
                    helper.setVisible(R.id.layout_item_dialog_title, false);
                    helper.setText(R.id.tv_item_dialog_name, item.getUserName());
                    helper.setText(R.id.tv_item_dialog_number, item.getUserId());
                    helper.setText(R.id.tv_item_dialog_phone, item.getPhone());
                }
                if (position == helper.getAdapterPosition()) {
                    check.setImageResource(R.mipmap.check_select);
                } else {
                    check.setImageResource(R.mipmap.check_normal);
                }
            }
        };
        adapter.setOnItemClickListener((adapter1, view, position1) -> {
            if (position1 != 0) {
                ListDialog.this.position = position1;
                adapter.notifyDataSetChanged();
            }
        });
        recyclerView.setHasFixedSize(false);
        recyclerView.setAdapter(adapter);
    }

    public void setOnItemClickL(OnItemClickL onItemClickL) {
        this.onItemClickL = onItemClickL;
    }
// --->属性设置

    /**
     * set style(设置style)
     */
    public ListDialog style(int style) {
        this.mStyle = style;
        return this;
    }

    /**
     * set title underline color(设置标题下划线颜色)
     */
    public ListDialog titleLineColor(int titleLineColor) {
        this.mTitleLineColor = titleLineColor;
        return this;
    }

    /**
     * set title underline height(设置标题下划线高度)
     */
    public ListDialog titleLineHeight(float titleLineHeight_DP) {
        this.mTitleLineHeight = titleLineHeight_DP;
        return this;
    }

    /**
     * set divider color between btns(设置btn分割线的颜色)
     */
    public ListDialog dividerColor(int dividerColor) {
        this.mDividerColor = dividerColor;
        return this;
    }

    public interface OnItemClickL {
        void onItemClick(NextPersonEntity bean, int position);
    }
}
